import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Regist from '../views/Regist.vue'
import Admin from '../views/Admin/Admin.vue'
import Merchant from '../views/Merchant/Merchant.vue'

import AdminIndex from '../views/Admin/AdminIndex.vue'
import AddAdminUser from '../views/Admin/User/AddAdminUser.vue'
import MerchantUser from '../views/Admin/User/MerchantUser.vue'
import MerchantList from '../views/Admin/Merchant/MerchantList.vue'
import MerchantOrderList from '../views/Admin/Merchant/MerchantOrderList.vue'

import MerchantIndex from '../views/Merchant/MerchantIndex.vue'
import AddShop from '../views/Merchant/Shop/AddShop.vue'
import ShopList from '../views/Merchant/Shop/ShopList.vue'
import Type from '../views/Merchant/Shop/Type.vue'
import OrderList from '../views/Merchant/Order/OrderList.vue'
import ChangeMerchant from '../views/Merchant/My/ChangeMerchant.vue'
import ChangePass from '../views/Merchant/My/ChangePass.vue'

import store from '../store/index'
Vue.use(VueRouter)
export const MerchantList1 = [
  {
    path: '/merchant',
    component: MerchantIndex,
    title:'首页'
  },
  {
    path: '/merchant/addshop',
    component: AddShop,
    title:'添加商品'
  },
  {
    path: '/merchant/shoplist',
    component: ShopList,
    title:'商品列表'
  },
  {
    path: '/merchant/type',
    component: Type,
    title:'商品分类'
  },
  {
    path: '/merchant/orderlist',
    component: OrderList,
    title:'订单列表'
  },
  {
    path: '/merchant/changemerchant',
    component: ChangeMerchant,
    title:'修改店铺'
  },
  {
    path: '/merchant/changepass',
    component: ChangePass,
    title:'修改密码'
  },
]
export const adminList = [
  {
    path: '/admin',
    component: AdminIndex,
    title:'首页'
  },
  {
    path: '/admin/addadminuser',
    component: AddAdminUser,
    title:'管理员账号'
  },
  {
    path: '/admin/merchantuser',
    component: MerchantUser,
    title:'商户账号'
  },
  {
    path: '/admin/merchantlist',
    component: MerchantList,
    title:'商户列表'
  },
  {
    path: '/admin/merchantorderlist',
    component: MerchantOrderList,
    title:'商户订单'
  },
]
const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    redirect:'/admin'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/regist',
    name: 'Regist',
    component: Regist
  },
  {
    path: '/admin',
    name: 'Admin',
    component: Admin,
    children:[...adminList]
  },
  {
    path: '/merchant',
    name: 'Merchant',
    component: Merchant,
    children:[...MerchantList1]
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  if(to.path=='/login'||to.path=='/regist'){
    if(store.state.user.userid){
      next(from.path)
    }else{
      next()
    }    
  }else{
    if(store.state.user.userid){
      next()     
    }else{
      next('/login')
    }  
  }
})
export default router
